package com.tianyuan.dplrf.ui.popup;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.base.BasePopupWindow;
import com.hjq.base.action.AnimAction;
import com.hjq.toast.ToastUtils;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tianyuan.dplrf.R;
import com.tianyuan.dplrf.app.AppAdapter;
import com.tianyuan.dplrf.ui.adapter.Ncjt_tianqiAdapter;
import com.tianyuan.dplrf.ui.bean.Ncjt_tianqibean;

import java.util.ArrayList;
import java.util.List;

import com.tianyuan.dplrf.wxapi.WeChatShareUtils;

/**
 * desc   : 分享对话框
 */
public final class SharePop2 {

    public static final class Builder
            extends BaseDialog.Builder<Builder>
            implements BaseAdapter.OnItemClickListener {

        private final RecyclerView mRecyclerView;
        private final ShareAdapter mAdapter;

        private final ShareBean mCopyLink;


        public Builder(Activity activity) {
            super(activity);

            setContentView(R.layout.share_dialog);

            final List<ShareBean> data = new ArrayList<>();
            data.add(new ShareBean(getDrawable(R.drawable.share_wechat_ic), getString(R.string.share_platform_wechat), SendMessageToWX.Req.WXSceneSession));
            data.add(new ShareBean(getDrawable(R.drawable.share_moment_ic), getString(R.string.share_platform_moment), SendMessageToWX.Req.WXSceneTimeline));

            mCopyLink = new ShareBean(getDrawable(R.drawable.share_link_ic), getString(R.string.share_platform_link), -1);
            data.add(mCopyLink);

            mAdapter = new ShareAdapter(activity);
            mAdapter.setData(data);
            mAdapter.setOnItemClickListener(this);

            mRecyclerView = findViewById(R.id.rv_share_list);
            mRecyclerView.setLayoutManager(new GridLayoutManager(activity, data.size()));
            mRecyclerView.setAdapter(mAdapter);

        }

        private String sharetext = "https://www.pgyer.com/android_tianyuan";

        private int shareMode = 0;

        /**
         * {@link BaseAdapter.OnItemClickListener}
         */
        @Override
        public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
            int platform = mAdapter.getItem(position).sharePlatform;
            if (platform != -1) {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.img_icon);
                WeChatShareUtils.getInstance(getActivity().getApplicationContext()).shareUrl(sharetext,"田园",bitmap,"一款田园生活体验app\n"
                        + "安装邀请码：1155",platform);
//                WeChatShareUtils.getInstance(getContext()).shareText(sharetext, platform);
            } else {
                // 复制到剪贴板
                getSystemService(ClipboardManager.class).setPrimaryClip(ClipData.newPlainText("url", sharetext));
                ToastUtils.show(R.string.share_platform_copy_hint);
            }
            dismiss();
        }


    }

    private static class ShareAdapter extends AppAdapter<ShareBean> {

        private ShareAdapter(Context context) {
            super(context);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder();
        }

        private final class ViewHolder extends AppAdapter<?>.ViewHolder {

            private final ImageView mImageView;
            private final TextView mTextView;

            private ViewHolder() {
                super(R.layout.share_item);
                mImageView = findViewById(R.id.iv_share_image);
                mTextView = findViewById(R.id.tv_share_text);
            }

            @Override
            public void onBindView(int position) {
                ShareBean bean = getItem(position);
                mImageView.setImageDrawable(bean.shareIcon);
                mTextView.setText(bean.shareName);
            }
        }
    }

    private static class ShareBean {

        /**
         * 分享图标
         */
        final Drawable shareIcon;
        /**
         * 分享名称
         */
        final String shareName;
        /**
         * 分享平台
         */
        final int sharePlatform;

        private ShareBean(Drawable icon, String name, int platform) {
            shareIcon = icon;
            shareName = name;
            sharePlatform = platform;
        }
    }
}
